package cn.juque.lucenecandy.core.datasync;

import cn.juque.lucenecandy.bo.IndexUpdateParamBO;
import cn.juque.lucenecandy.cache.DataSyncWaitReadMsgCache;
import cn.juque.lucenecandy.core.annotation.DataSync;
import cn.juque.lucenecandy.core.datasync.listener.CommitListenerRender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>MsgDataSyncServiceImpl</li>
 * </ul>
 * @date 2024-01-02
 **/
@Slf4j
@Service("msgDataSyncService")
@DataSync("msg")
public class MsgDataSyncServiceImpl implements IDataSyncService{

    @Resource
    private DataSyncWaitReadMsgCache dataSyncWaitReadMsgCache;

    @Resource(name = "defaultDataSyncService")
    private IDataSyncService dataSyncService;

    @Resource
    private CommitListenerRender commitListenerRender;

    /**
     * 更新
     *
     * @param param 数据
     */
    @Override
    public void commit(IndexUpdateParamBO param) {
        param.setTimestamp(System.currentTimeMillis());
        this.dataSyncService.commit(param);
        if(!this.commitListenerRender.before(param)) {
            return;
        }
        this.dataSyncWaitReadMsgCache.put(null, param);
        this.commitListenerRender.after(param);
    }

    /**
     * 删除
     *
     * @param param 数据
     */
    @Override
    public void cancel(IndexUpdateParamBO param) {
        // do nothing
    }
}
